<template>
  <view>
    <!--不要公共模块-->
	<u-gap height="90"></u-gap>
    <view class="">
      <view style="width: 80px;margin: auto;">
		  <u--image src="https://i.yodin.com/storage/app/logo.png" width="80px" height="80px"></u--image>
	  </view>
	
	  <view style="width: 90%;padding:20px 5% 0px 5%;text-align: center;">如果跳出登陆来到此页，请关闭页面，重新进入，或刷新小程序。</view>
    </view>
	
    <view class="u-padding-35">

		<u--form
				labelPosition="left"
				:model="model"
				:rules="rules"
				ref="uForm"
				labelWidth="60"
		>
        <u-form-item  label="用户名" prop="username">
			<u--input v-model="model.username" placeholder="请填写用户名"  type="text"/>
		</u-form-item>
        <u-form-item  label="密码" prop="password">
			<u--input v-model="model.password" placeholder="请填写密码" type="password"/>
        </u-form-item>
      </u--form>

      <u-gap height="20"></u-gap>

      <u-button type="primary" @click="submit()" :disabled="loadstatus" :loading="loadstatus" loadingText="login..">登陆</u-button>
      
      <u-gap height="20"></u-gap>

      <view class="">
        <!-- <view class="u-flex-1 u-text-left" @click="$Router.push('/pages/account/login/sms-login')">验证码登陆</view> -->
        <view class="" @click="$Router.push('/pages/componentsC/account/register/username-register')">注册</view>
        <!-- <view class="u-flex-1 u-text-right" @click="$Router.push('/pages/account/findPassword/mobile-findPassword')">忘记密码</view> -->
      </view>
    </view>

    <u-gap height="40"></u-gap>
  </view>
</template>

<script>
import {mapState,mapMutations} from 'vuex';
export default {
  data() {
    return {
      loadstatus: false,
	  loadstxt:'loading',
      model: {
        /* 账号 */
        username: "",
        /* 密码 */
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: ["change", "blur"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["change", "blur"],
          },
          {
            min: 6,
            max: 20,
            message: '密码长度在6到20个字符',
            trigger: ["change", "blur"],
          },
          {
            pattern: /^[0-9a-zA-Z]*$/g,
            transform(value) {
              return String(value);
            },
            message: '密码为字母或数字',
            trigger: ["change", "blur"],
          },
        ],
      },
    };
  },
  computed: {
  	...mapState(['user'])
  },
  onLoad(){
		this.hasLogin=this.$store.getters.loginFlag;
		if (!this.hasLogin){
			// #ifdef MP-WEIXIN
			this.judgeLogin(() => {})
			// #endif
		}
  },
  methods: {
    submit() {
		this.$store.commit("OUT_LOGIN");
		this.$refs.uForm.validate().then(res => {
			this.loadstatus = true;
			setTimeout(() => {
				return new Promise((resolve, reject) => {
				  this.$api("user.login", {
					logintype: "login_by_username", //login_by_mobile
					username: this.model.username,
					password: this.model.password,
					bid:this.user.bid
				  }).then((res) => {
					  if(res.code==200){
						  this.$store.commit("USER_INFO", res.data);
						  this.$store.commit("TOKEN_INFO", res.data.member_token);
						  uni.setStorageSync("fromLogin", {path:'/pages/index/my',query:''}); //要跳转到的页面
						  this.$store.dispatch(
							"setTokenAndBack",
							res.data.member_token
						  );
							let bussiness = {
								bid:res.data.bid,
							};
							this.$store.commit("setbasebusinessID",bussiness);
							resolve(true);
					  }else{
							//let data = {
							//	bid:'',
							//};
							//this.$store.commit("setbasebusinessID",data);
							this.$store.commit('OUT_LOGIN');
					  }
					  this.loadstatus = false;
					}).catch((e) => {
					  this.loadstatus = false;
					  resolve(false);
					});
				});
			  }, 300);
		}).catch(errors => {

		})

    },
  },

};
</script>

<style lang="scss" scoped>

</style>
